#include "StdAfx.h"
#include ".\myatlimage.h"

CMyAtlImage::CMyAtlImage(void)
{
}

CMyAtlImage::~CMyAtlImage(void)
{
}



void CMyAtlImage::SetImageBackColor( CImage* img,COLORREF color )
{
	SetBkColor(img->GetDC(),color);
	img->ReleaseDC();
	RECT rc;
	rc.left=0;
	rc.right=img->GetWidth();
	rc.top=0;
	rc.bottom=img->GetHeight();
	::ExtTextOutA(img->GetDC(), 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
	img->ReleaseDC();
}

void CMyAtlImage::PNGImageAlpha( CImage* pPngImg )
{
	if (pPngImg->GetBPP()>=32)
	{
		for (int i=0;i<pPngImg->GetWidth();++i)
		{
			for (int j=0;j<pPngImg->GetHeight();++j)
			{
				unsigned char* pucColor=(unsigned char*)pPngImg->GetPixelAddress(i,j);
				pucColor[0]=pucColor[0]*pucColor[3]/255;
				pucColor[1]=pucColor[1]*pucColor[3]/255;
				pucColor[2]=pucColor[2]*pucColor[3]/255;
			}
		}
	}
}

BOOL CMyAtlImage::Load886( LPCTSTR pszFileName )
{
	if (FAILED(ATL::CImage::Load(pszFileName)))
	{
		return FALSE;
	}
	PNGImageAlpha(this);

	return TRUE;
}

void CMyAtlImage::SetBackColor886( COLORREF color )
{
	SetImageBackColor(this,color);
}

BOOL CMyAtlImage::LoadFormMem( void * pMemData, long len )
{
	HGLOBAL hGlobal  =  GlobalAlloc(GMEM_MOVEABLE, len);
	void *  pData  =  GlobalLock(hGlobal);
	memcpy(pData, pMemData, len);
	GlobalUnlock(hGlobal);

	IStream *  pStream  =  NULL;
	if  (CreateStreamOnHGlobal(hGlobal, TRUE,  & pStream)  ==  S_OK)
	{
		if (FAILED(this->Load(pStream)))
		{
			return FALSE;
		}
		PNGImageAlpha(this);
		pStream -> Release();    
	} 
	GlobalFree(hGlobal);



	return TRUE;
}

